<template>
	<div class="mainDiv" style="padding: 20px;box-sizing: border-box;">
        <el-col :span="24">
                <el-input placeholder="请输入管理员真实姓名" v-model="name" style="margin: 5px 0">
                    <template slot="prepend">姓名</template>
                </el-input>

                <el-input placeholder="请输入管理员手机号" v-model="phone" style="margin: 5px 0">
                    <template slot="prepend">手机</template>
                </el-input>

                <el-input placeholder="请输入管理员登录帐号" v-model="username" style="margin: 5px 0">
                    <template slot="prepend">帐号</template>
                </el-input>

                <el-input type="password" placeholder="请输入管理员登录密码" v-model="password" style="margin: 5px 0">
                    <template slot="prepend">密码</template>
                </el-input>

                <el-input type="password" placeholder="请再次输入登录密码" v-model="password1" style="margin: 5px 0">
                    <template slot="prepend">确认密码</template>
                </el-input>

                <el-input type="textarea" placeholder="请输入管理员描述（操作功能）" v-model="remarks" style="margin: 5px 0"></el-input>

            </el-col>

            <el-row>
            <el-col :span="12">
                <el-button @click.native="close" style="width:90%;display:block;">取消</el-button>
            </el-col>
            <el-col :span="12">
                <el-button type="success" @click.native="callSubmit" style="width:90%;display:block;" :loading="loading">确定</el-button>
            </el-col>
        </el-row>

    </div>
</template>
<script>
import C from '@/common/common.js'
export default {
  name: 'groupAdd',
  data () {
    return {
      id: 0,
      name: '',
      phone: '',
      username: '',
      password: '',
      password1: '',
      remarks: '',
      loading: false
    }
  },
  mounted () {
    let RequestUrl = C.getDatas()
    if (RequestUrl.row) {
      let row = RequestUrl.row
      this.id = row.id
      this.name = row.name
      this.phone = row.phone
      this.username = row.username
      this.remarks = row.remarks
    }
  },
  methods: {
    async callSubmit (index) {
      if (!(/^[\u4e00-\u9fa5]{2,4}$/.test(this.name))) {
        C.error('姓名为2到4个汉字')
        return false
      } else if (!(/^1[345678]\d{9}$/.test(this.phone))) {
        C.error('手机号不正确')
        return false
      } else if (!(/^[a-zA-Z0-9]{6,}$/.test(this.username))) {
        C.error('帐号只能为字母和数字且不能少于6位')
        return false
      } else if (/^\s*$/.test(this.password)) {
        C.error('请输入账户密码')
        return false
      } else if (this.password !== this.password1) {
        C.error('两次密码输入不一致')
        return false
      }

      let data = {
        id: this.id,
        name: this.name,
        username: this.username,
        phone: this.phone,
        password: this.password,
        remarks: this.remarks
      }
      this.loading = true
      let res = await C.ajax('/organization/group/addadmin', data, 'post')
      if (res) {
        if (res.errno === 0) {
          this.loading = false
          this.$notify({
            title: '成功',
            message: res.data,
            type: 'success'
          })
          C.closeBrowserWindow(res)
        } else {
          this.loading = false
          this.$notify.error({
            title: '错误',
            message: res.errmsg
          })
        }
      }
    },
    close () {
      C.closeBrowserWindow()
    }
  }
}
</script>